package com.yayo.mangement.rest;

import com.alibaba.fastjson.JSON;
import com.yayo.mangement.utils.IOStreamUtils;
import com.yayo.mangement.resp.WorkbookResp;
import com.yayo.mangement.service.XlsxExportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Controller
@Slf4j
@RequestMapping("/api/sys/xlsx")
public class XlsxExporterController {

    @Autowired
    private XlsxExportService xlsxExportService;

    @PostMapping("/export/{name}")
    public void export(@PathVariable String name, @RequestBody Object exportContext, HttpServletResponse response){
        String exportContextJson = JSON.toJSONString(exportContext);
        WorkbookResp exporter = xlsxExportService.exporter(name, exportContextJson);
        IOStreamUtils.output(exporter.getFileName(),exporter.getWorkbook(), response);
    }

}
